package org.news.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.news.web.constant.CategoriesStatus;
import org.news.web.constant.SubsciptionsConstant;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.SubscriptionsVo;
import org.news.web.domain.bo.SubscriptionsBo;
import org.news.web.service.ISubscriptionsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 订阅功能
 *
 * @author Gu Ci
 * @date 2024-04-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/subscriptions")
public class SubscriptionsController extends BaseController {

    private final ISubscriptionsService subscriptionsService;

    /**
     * 查询订阅功能列表
     */
    @SaCheckPermission("web:subscriptions:list")
    @GetMapping("/list")
    public TableDataInfo<SubscriptionsVo> list(SubscriptionsBo bo, PageQuery pageQuery) {
        return subscriptionsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订阅功能列表
     */
    @SaCheckPermission("web:subscriptions:export")
    @Log(title = "订阅功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SubscriptionsBo bo, HttpServletResponse response) {
        List<SubscriptionsVo> list = subscriptionsService.queryList(bo);
        ExcelUtil.exportExcel(list, "订阅功能", SubscriptionsVo.class, response);
    }

    /**
     * 获取订阅功能详细信息
     *
     * @param subscriptionId 主键
     */
    @SaCheckPermission("web:subscriptions:query")
    @GetMapping("/{subscriptionId}")
    public R<SubscriptionsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long subscriptionId) {
        return R.ok(subscriptionsService.queryById(subscriptionId));
    }

    /**
     * 新增订阅功能
     */
    @SaCheckPermission("web:subscriptions:add")
    @Log(title = "订阅功能", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SubscriptionsBo bo) {
        return toAjax(subscriptionsService.insertByBo(bo));
    }

    /**
     * 删除订阅功能
     *
     * @param subscriptionIds 主键串
     */
    @SaCheckPermission("web:subscriptions:remove")
    @Log(title = "订阅功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{subscriptionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] subscriptionIds) {
        return toAjax(subscriptionsService.deleteWithValidByIds(List.of(subscriptionIds), true));
    }

    /**
     * 获取订阅状态
     */
    @GetMapping("/typeStatus")
    public R<List<CategoriesStatus>> typeStatus(){
        return R.ok(SubsciptionsConstant.SC_STATUSES);
    }
}
